import * as appConfig from '../../../app-config.js';
var app = getApp()
Page({
  data: {
    topCurrentId:'all',
    leftCurrentId:0,
    settlement:'兑换单',
    leftBtn:[],
    topBtn:[],
    list:[],
    backBox:[],
    parent_id:0,
    goodsType:'',
    addImg:'../../../image/add.png',
    priceName:'零售价',
    agentId:'',
    page:2,
    noData:false,
    banner:'',
    distribution:[],
    settlementCount:0,
    accountId:'',
    // topCurrentIndex:"0"
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getDistribution()
      // this.getFirstTypeList(res.data.agentId)
      // this.getGoodsTypeList(res.data.agentId,0)
      this.getSettlement(res.data.id)
    })
    this.banner()
  },
  onShow: function() {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getDistribution()
      this.getSettlement(res.data.id)
      this.setData({
        leftCurrentId:0
      })
      // this.getFirstTypeList(res.data.agentId)
      // this.getGoodsTypeList(res.data.agentId,0)
    })
  },
  //商品种类选择事件
  bindLeftBtn(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      leftCurrentId:e.currentTarget.dataset.id,
      goodsType:type,
      page:2
    })
    this.getGoodsList('1')
  },
  //结算
  bindToSettlement(){
    wx.navigateTo({
      url: 'replaceSettlement?type=1'
    })
  },
  //商品详情
  bindToDetail(e){
    wx.navigateTo({
      url:'replaceDetail?type=1&goodsType='+e.currentTarget.dataset.type+
      '&id='+e.currentTarget.dataset.id+
      '&price='+e.currentTarget.dataset.price+
      '&company='+e.currentTarget.dataset.company+
      '&color='+e.currentTarget.dataset.color
    })
  },
  //获取商品列表
  getGoodsList(page){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      searchKey:''
    }
    console.log(data)
    let list = []
    app.request('wx_app/mall/client/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.name,
            price:item.price,
            url:item.url,
            id:item.sku_id,
            companyName:item.product_tag,
            color:item.tag_color,
            product_type:item.product_type,
            versionNameId:item.versionNameId,
            versionValues:item.versionValues,
            apply_to:item.apply_to,
            mt_id:item.mt_id
          })
          that.setData({
            list
          })
        })

        wx.hideLoading()
        if (that.data.list.length==0) {
          that.setData({
            noData:true
          })
        }else{

          that.setData({
            noData:false
          })
        }
      }else{
        that.setData({
          noData:true
        })
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  loadMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      searchKey:''
    }

    let list = this.data.list
    app.request('wx_app/mall/client/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.name,
            price:item.price,
            url:item.url,
            id:item.sku_id,
            companyName:item.product_tag,
            color:item.tag_color,
            product_type:item.product_type,
            versionNameId:item.versionNameId,
            versionValues:item.versionValues,
            apply_to:item.apply_to,
            mt_id:item.mt_id
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        res.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
  //获取一级分类
  getFirstTypeList(agentId){
    const that = this
    let topBtn = this.data.topBtn
    let data = {
      agentId,
      requestType:'client'
    }
    
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0) {
          topBtn.push({name:item.name,id:item.id})
        }
      })
      that.setData({
        topBtn,
        page:2
      })
    })
  },
  chooseAll(){
    this.setData({
      topCurrentId:'all',
      parent_id:0,
      leftCurrentId:0
    })
    this.getGoodsTypeList(this.data.agentId,0)
  },
  chooseFirstType(e){
    let id = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    this.setData({
      topCurrentId:id,
      parent_id:type,
      leftCurrentId:0
    })
    this.getGoodsTypeList(this.data.agentId,type)
  },
  //获取商品种类
  getGoodsTypeList(agentId,type){
    const that = this
    let leftBtn = []
    let backBox = []
    let goodsType=''
    let data = {
      agentId,
      requestType:'client'
    }
    console.log(type)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (type==item.parent_id&&type!=0) {
          leftBtn.push({name:item.name,id:item.id})
        }else if (type==0) { //全部
          if (item.parent_id!=0) {
            leftBtn.push({name:item.name,id:item.id})
          }
        }
        if (type==item.id) { 
          backBox.push({name:item.name,id:item.id})
        }
      })
      that.setData({
        leftBtn,
        backBox
      })
      if (that.data.leftBtn==''&&that.data.topCurrentId=='all') {
        res.data.product_types.forEach(item=>{
          leftBtn.push({name:item.name,id:item.id})
        })
        that.setData({
          leftBtn
        })
      }
      if (that.data.leftBtn=='') {
        that.setData({
          leftBtn:that.data.backBox
        })
      }
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2
      })
      that.getGoodsList('1')
    })
  },
  bindSearch(){
    wx.navigateTo({
      url:'replaceSearch'
    })
  },
  banner(){
    let data = {}
    app.request('wx_app/banner',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {

        this.setData({
          banner:res.data[0].imgUrl
        })

      }else{

      }
    })
  },
  //获取配送商品
  getDistribution(e){
    // if (e) {
    //   this.setData({
    //     topCurrentIndex:e.currentTarget.dataset.index
    //   })
    // }
    let that = this
    let distribution = this.data.distribution
    let leftBtn = []
    let goodsType=''
    let data = {
      agentId:this.data.agentId,
      requestType:'client'
    }
    console.log(data)
    app.request('wx_app/mall/product/getOldForNewTypes',data,'GET').then(res=>{
      console.log(res)
      res.data.forEach(item=>{   
        leftBtn.push({name:item.name,id:item.id})
      })
      that.setData({
        leftBtn
      })
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2
      })
      that.getGoodsList('1')
    })
  },
  //获取自营商品
  getSelfSupport(e){
    // this.setData({
    //   topCurrentIndex:e.currentTarget.dataset.index,
    //   list:[]
    // })
    let that = this
    let distribution = this.data.distribution
    let leftBtn = []
    let goodsType=''
    let data = {
      agentId:this.data.agentId,
      requestType:'client'
    }
    console.log(data)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0) {
          if (item.id==1) {
            leftBtn.push({name:item.name,id:item.id})
          }
        }
      })

      that.setData({
        leftBtn
      })
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2,
      })
      that.getGoodsList('1')
    })
  },
  statistics(){
    wx.redirectTo({
      url:'replaceStatistics'
    })
  },
  //获取购物车数量
  getSettlement(accountId){
    let data = {
      accountId,
      isOldForNew:0
    }
    console.log(data)
    var that = this
    app.request('wx_app/oldForNew/cart/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let count = 0
        res.data.carts.forEach(item=>{
          count+=item.count
        })
        this.setData({
          settlementCount:count
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  addGoods(e){
    let current = e.currentTarget.dataset
    let data = {
      productType:current.type,
      mtId:current.mtId,
      applyTo:current.applyTo,
      accountId:this.data.accountId,
      skuId:current.id,
      productName:current.name,
      price:current.price,
      count:1,
      versionValues:current.vv,
      versionNameId:current.vId,
      isOldForNew:0
    }
    console.log(data)
    app.request('wx_app/oldForNew/cart/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showToast({
          title:'加入购物单成功！'
        })
        this.getSettlement(this.data.accountId)
      }else{
        app.errorMsg(res.message)
      }
    })
  }
})